var url;
$(document).ready(function(){
	validateModifyForm();
})

/**
 * 分页控制
 * @param frm 控制的目标表单
 * @param index	当前页下标
 * @returns
 */
function page_nav(frm, index){
	frm.pageIndex.value = index;
	frm.submit();
}


function validateModifyForm() {

	$("#modifyForm").validate({
		onsubmit:true,// 是否在提交是验证
	　　	onfocusout:false,// 是否在失去焦点时验证
	　　	onkeyup :false,// 是否在敲击键盘时验证
		rules:{
			goodsName: {
				required: true,
			},
			num: {
				required: true,
				range:[1, 200]
			},
			unit: {
				required: true,
			},
			sellPrice:{
				required: true,
				priceFormat: true
			},
			fixPrice:{
				required: true,
				priceFormat: true
			}
		},
		messages: {
			goodsName: {
				required: "物资名称必填",
			},
			num: {
				required: "物资数量必填",
				range: "物资数量要求1-200之间"
			},
			unit: {
				required: "单位必填",
			},
			sellPrice:{
				required: "建议零售价必填",
			},
			fixPrice:{
				required: "进价必填",
			}
		},
		
		errorPlacement: function(error, element){
			$("#modify-errtip").show();
			$("#modify-errtip").removeClass("alert-info").addClass("alert-danger");
			$("#modify-errtip").html(error);
		},
		submitHandler: function(form){
			form.submit();
		},
		invalidHandler: function(form, validator) {
			return false;}
		
	})
	
	 jQuery.validator.addMethod("priceFormat",function(value,element){
		 var reg = /^([1-9][0-9]*)+(.[0-9]{1,2})?$/;
         return this.optional(element) || reg.test(value);
      }, "请输入合法的金额(例如:98/98.0/98.55");
}



/**
 * 物资修改页面
 */
$(".goodsedit").on("click", function(e){
	url = $(this).attr("url");
	var obj = $(this);
	var goodsCode = obj.attr("goods-code");
	$.get(url + "basic/goods/modify", {"goodsCode": goodsCode}, function(result){
		console.log(result);
		var data = JSON.parse(result.msg);
		if (data != null && data != "fail") {
			//将值赋给模态框
			$("#m_goodsCode").val(data.goodsCode);
			$("#m_goodsName").val(data.goodsName);
			$("#m_type").val(data.typeName);
			$("#m_num").val(data.num);
			$("#m_unit").val(data.unit);
			$("#m_sellPrice").val(data.sellPrice);
			$("#m_fixPrice").val(data.fixPrice);
			$("#m_desc").val(data.desc);
		} else {
			alert("无法获取物资"+ goodsCode +"信息,进行修改!");
		}
	}, "json");
	
	
	//打开模态框
	e.preventDefault();
	$('#modifyGoods').modal('show');
	
})

$(".goodsview").on("click", function(e){
	url = $(this).attr("url");
	var obj = $(this);
	var goodsCode = obj.attr("goods-code");
	$.get(url + "basic/goods/view", {"goodsCode": goodsCode}, function(result){
		console.log(result);
		var data = result.msg;
		if (data != null && data != "fail") {
			//将值赋给模态框
			$("#v_goodsCode").val(data.goodsCode);
			$("#v_goodsName").val(data.goodsName);
			$("#v_type").val(data.typeName);
			$("#v_num").val(data.num);
			$("#v_unit").val(data.unit);
			$("#v_sellPrice").val(data.sellPrice);
			$("#v_fixPrice").val(data.fixPrice);
			$("#v_desc").val(data.desc);
			$("#v_createBy").val(data.createBy);
			$("#v_createDate").val(data.createDate);
		} else {
			alert("无法查看物资"+ goodsCode +"信息");
		}
	}, "json");
	
	
	//打开模态框
	e.preventDefault();
	$('#viewGoods').modal('show');
	
})



///留给出库

/**
 * 删除模态框的显示
 */
$(".goodsdel").on("click", function(e){
	var goodsCode = $(".goodsdel").attr("goods-code");
	url =  $(this).attr("url");
	
	$("#d_goodsCode").val(null);
	$("#delInfo").html("您正在删除物资编号为【 " + goodsCode + "】的记录!<br>该操作将导致库存数据同步清理, 是否继续?");
	
	e.preventDefault();
	$('#delGoods').modal('show');
})

/**
 * 执行删除
 */
$("#delBtn").click(function(){
	var goodsCode = null;
	var obj = $(".deltip");
	var tip;
	
	$.get(url + "basic/goods/del", {"goodsCode": goodsCode}, function(result){
		if (result.msg == "success") {
			tip = "{\"text\":\"删除成功\", \"layout\":\"topLeft\",\"type\":\"success\"}";
			obj.attr("data-noty-options", tip);
			$(".deltip").trigger("click");
			$(window).attr('location',  url + "base/goodlist.html");
		} else if(result.msg == "fail") {
			tip = "{\"text\":\"不存在该物资,删除失败\", \"layout\":\"center\",\"type\":\"error\"}";
			
		} else {
			tip = "{\"text\":\""+ result.error +"\", \"layout\":\"center\",\"type\":\"error\"}";
		}
		
		obj.attr("data-noty-options", tip);
		$(".deltip").trigger("click");
	}, "json");
	
})


